package business.global.pk.tdk;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import jsproto.c2s.cclass.pk.BasePocker;
import jsproto.c2s.cclass.pk.BasePockerLogic;

/**
 * 吹牛，设置牌
 *
 * @author Huaxing
 *
 */
public class TDKSetCard {
	public final static int CARD_SIZE_1 = 1;
	public final static int CARD_SIZE_2 = 2;
	public ArrayList<Integer> leftCards = new ArrayList<Integer>(); // 扑克牌编号
	private Random random;

	private int lasCard=-1;
	public TDKSetCard(TDKRoomSet ySet) {
		this.random = new Random();
		// 获取两副牌，2大，包含大小王
		this.leftCards = BasePockerLogic.getRandomPockerList(1, 1, BasePocker.PockerListType.POCKERLISTTYPE_TWOEND);

		int paishu=ySet.getRoom().getPaiShu();
		if(TDKdefine.TDK_PAISHU_TYPE.PS_5_9TA.ordinal()==paishu){
			this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCard3T8A2List));
		}else if(TDKdefine.TDK_PAISHU_TYPE.PS_5_10TA.ordinal()==paishu){
			this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCard3T9A2List));
		}else if(TDKdefine.TDK_PAISHU_TYPE.PS_7_7TA.ordinal()==paishu){
			this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCard3T6A2List));
		}else if(TDKdefine.TDK_PAISHU_TYPE.PS_7_8TA.ordinal()==paishu){
			this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCard3T7A2List));
		}else if(TDKdefine.TDK_PAISHU_TYPE.PS_7_2TA.ordinal()==paishu){
			//this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCard2List));
		}else if(TDKdefine.TDK_PAISHU_TYPE.PS_4_9TA.ordinal()==paishu){
			this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCard3T8A2List));
		}else if(TDKdefine.TDK_PAISHU_TYPE.PS_4_JTA.ordinal()==paishu){
			this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCard3T10A2List));
		}
		if(!ySet.getRoom().keXuanWanFa(TDKdefine.TDK_KEXUAN.SHUANGWANG)) {
			this.leftCards.removeAll(Arrays.asList(TDKdefine.OutCardShuangWang));
		}
		if(ySet.getRoom().keXuanWanFa(TDKdefine.TDK_KEXUAN.DAI2) && TDKdefine.TDK_PAISHU_TYPE.PS_7_2TA.ordinal()!=paishu){
			this.leftCards.addAll(Arrays.asList(TDKdefine.OutCard2List));
		}

		this.onXiPai();
	}


	public void clean() {
		if (null != leftCards) {
			this.leftCards.clear();
			this.leftCards = null;
		}
		this.random = null;
	}


	/**
	 * 洗牌
	 **/
	public void onXiPai() {
		Collections.shuffle(this.leftCards);
	}

	/**
	 * 神牌
	 *
	 * @param cardIntList
	 * @return
	 */
	public ArrayList<Integer> godPopList(List<Integer> cardIntList) {
		ArrayList<Integer> cardList = new ArrayList<>();
		for (Integer cardInt : cardIntList) {
			if (this.leftCards.remove(cardInt)) {
				cardList.add(cardInt);
			}
		}
		return cardList;
	}

	/**
	 * 获取随机下一个值
	 * @param size
	 * @return
	 */
	public int nextInt (int size) {
		return random.nextInt(size);
	}

	/**
	 * 获取单张牌
	 * @param isLast 是否是最后一个发牌，如果是那下轮大家都不用发了
	 * @return
	 */
	public TDKCard popCard (boolean isLast) {
		if (this.lasCard!=-1) {
			return new TDKCard(this.lasCard,true);
		}else if(this.leftCards.size() <= 0){
			return new TDKCard(0);
		}
		//测试功能取最后几张 panvc
		int size = this.leftCards.size() == 1 ? this.leftCards.size() : this.leftCards.size() -1;
		int cardID = this.leftCards.remove(random.nextInt(size));
		if(!isLast&&this.leftCards.size()<=0){
			this.lasCard=cardID;
		}
		return new TDKCard(cardID);
	}

	/**
	 * 获取单张牌
	 * @return
	 */
	public int popCard (Integer cardType) {
		if (this.leftCards.size() <= 0) {
			return 0;
		}
		boolean isCard = this.leftCards.remove(cardType);
		if (isCard) {
			return cardType;
		} else {
			return 0;
		}
	}

	/**
	 * 获取单张牌
	 * @return
	 */
	public TDKCard popCard (Integer cardType, boolean isLast) {
		if (this.lasCard != -1) {
			return new TDKCard(this.lasCard, true);
		}
		if (this.leftCards.size() <= 0) {
			return new TDKCard(0);
		}
		boolean isCard = this.leftCards.remove(cardType);
		if (isCard) {
			if(!isLast && this.leftCards.size() <= 0){
				this.lasCard = cardType;
			}
			return new TDKCard(cardType);
		} else {
			return this.popCard(isLast);
		}
	}


	/**
	 * 发牌
	 *
	 * @param cnt
	 * @return
	 */
	public ArrayList<Integer> popList(int cnt) {
		ArrayList<Integer> ret = new ArrayList<Integer>();
		if (this.leftCards.size() <= 0) {
			return ret;
		}
		for (int i = 0; i < cnt; i++) {
			if (this.leftCards.size() <= 0) {
				return ret;
			}
			Integer card = this.leftCards.remove(random.nextInt(this.leftCards.size()));
			ret.add(card);
		}
		return ret;
	}

	/**
	 * 取剩余牌的个数
	 */
	public int getLeftCardCount(){
		if(this.leftCards==null){
			return 0;
		}
		return this.leftCards.size();
	}

}
